package 力扣;

public class Leedcode50Powxn {

    public static void main(String[] args) {
        Leedcode50Powxn o = new Leedcode50Powxn();
        System.out.println(o.myPow(2.00000
                ,1));
    }
    /**
     * 传入x，n我就能求出x^n；
     * @param x
     * @param N
     * @return
     */
    public double myPow(double x, int N) {
        if (N == 1){
            return x;
        }
        if(N == 0){
            return 1;
        }
        if (Math.abs(x) == 1.0 && N % 2 == 1){
            return x;
        }
        if (Math.abs(x) == 1.0 && N % 2 == 0){
            return Math.abs(x);
        }
        long n = N;
        return n > 0 ? myPow1(x,n) : 1.0/myPow1(x,-n);
    }

    private double myPow1(double x, long n) {
        double ret ;
        if (n == 1) {
            return x;
        }
        ret = myPow1(x,n/2);
        return  n % 2 == 0 ? ret * ret : ret * ret * x;
    }
}
